package com.xenos.myPoker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

/**
 * 洗牌随机，顺序取牌
 * 
 * @author Administrator
 *
 */
public class GameMainBeta3 {
	private List<Player> playerList;
	private List<Poker> pokerList; // 存放一副卡牌（未洗）
	private int definePlayerNum;
	private int defineHandCardNum;// 每个玩家要抽取的卡牌张数
	Scanner scanner;

	public GameMainBeta3() {
		definePlayerNum = 3;
		defineHandCardNum = 5;
		pokerList = Poker.makePoker();// Poker用静态方法创建是因为Poker是一定的
		playerList = new ArrayList<>();
		scanner = new Scanner(System.in);
	}

	public GameMainBeta3(int definePlayerNum, int defineHandCardNum) {
		this.definePlayerNum = definePlayerNum;
		this.defineHandCardNum = defineHandCardNum;
		pokerList = Poker.makePoker();
		playerList = new ArrayList<>();
		scanner = new Scanner(System.in);
	}

	/**
	 * 创建玩家
	 */
	public void createPlayers() {
		int id;
		String name;
		int playerNum = definePlayerNum; // 循环变量
		int index = 1; // 创建第几位玩家
		while (playerNum > 0) {
			System.out.println("开始创建第" + index + "位玩家");
			System.out.println("请输入玩家编号:");
			try {
				id = scanner.nextInt();
				if (index > 1 && playerList.get(playerList.size() - 1).getID() == id) { // 控制重复id
					System.out.println("编号重复，请重新输入！");
					continue;
				}
			} catch (InputMismatchException e) {// 输入编号的格式异常处理
				System.out.println("非int编号，请重新输入！");
				scanner.next(); // 【吸收缓冲区】不然会死循环
				continue;
			}
			System.out.println("输入玩家姓名:");
			name = scanner.next();
			playerList.add(new Player(id, name)); // 添加玩家从这里可以看出复用性了！先不急于填充域，都获取再填充。
			System.out.println("第" + index + "位玩家创建成功！");
			playerNum--;
			index++;
		}
	}

	/**
	 * 另一种方法是(利用洗牌随机)
	 */
	public void shufflePoker() {
		System.out.println("------------开始洗牌------------");
		Collections.shuffle(pokerList);
		System.out.println(pokerList);
		System.out.println("------------洗牌结束------------");
	}

	/**
	 * 顺序取牌
	 */
	public void compareMaxPoker() {
		System.out.println("------------开始发牌-----------");
		int cardNums = definePlayerNum * defineHandCardNum;

		for (int i = 0; i < cardNums; ++i) {
			Poker poker = pokerList.get(i);
			playerList.get(i % definePlayerNum).getPlayerPokerList().add(poker); // i%PlayerNum神来之笔！
		}

		for (int j = 0; j < definePlayerNum; j++) {
			System.out.println(
					"玩家 " + playerList.get(j).getName() + " 有以下卡牌:  " + playerList.get(j).getPlayerPokerList());
		}
		System.out.println("-----------发牌结束------------");

		System.out.println("-----------开始游戏------------");
		int winner = 0;
		Poker maxPoker = playerList.get(0).getPlayerPokerList().get(0);
		for (int i = 0; i < definePlayerNum; ++i) {
			// 对玩家自己的手牌进行排序
			Collections.sort(playerList.get(i).getPlayerPokerList());
			// 对排序后的手牌进行逆序，get(0)即大牌，方便书写
			Collections.reverse(playerList.get(i).getPlayerPokerList());
			// 迭代后swap值(交换),通过迭代每一个玩家的大牌比一遍
			if (maxPoker.compareTo(playerList.get(i).getPlayerPokerList().get(0)) < 0) {
				maxPoker = playerList.get(i).getPlayerPokerList().get(0);
				winner = i;
			}
		}
		System.out.println("恭喜玩家" + playerList.get(winner).getName() + "获胜，其最大的卡牌为" + maxPoker);
		System.out.println("-----------游戏结束------------");
	}

	public static void main(String[] args) {
		System.out.println("-------------创建扑克牌-------------");
		GameMainBeta3 gameMainBeta3;
		if (args.length == 0) {
			gameMainBeta3 = new GameMainBeta3();
		} else if (args.length == 2) {
			// 参数设置异常处理，人数大于1小于等于52，取牌数大于0小于等于26,抽到的总牌数不超过52
			if ((Integer.parseInt(args[0]) <= 1) || (Integer.parseInt(args[0]) > 52) || (Integer.parseInt(args[1]) < 1)
					|| (Integer.parseInt(args[1]) > 26)
					|| (Integer.parseInt(args[0]) * Integer.parseInt(args[1]) > 52)) {
				System.out.println("*****参数设置错误，请重新设置玩家数或取牌数*****");
				return;
			}
			gameMainBeta3 = new GameMainBeta3(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
		} else {
			System.out.println("Usuage: java BetaGameMain [definePlayerNum] [defineHandCardNum]");
			return;
		}
		gameMainBeta3.createPlayers();
		gameMainBeta3.shufflePoker();
		gameMainBeta3.compareMaxPoker();
	}
}
